package com.fun.easyplugin;

import com.fun.easyplugin.event.listener.MyEventListener;
import com.fun.easyplugin.spring.SpringMainConfig;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class EasyPlugin extends JavaPlugin {

    @Getter
    private AnnotationConfigApplicationContext applicationContext;

    public EasyPlugin() {
        this.initSpringContext();
    }

    @Override
    public void onEnable() {
        // Plugin startup logic
        // 注册事件处理器
        Bukkit.getPluginManager()
                .registerEvents(applicationContext.getBean(MyEventListener.class), this);
    }

    /**
     * 初始化spring容器
     */
    private void initSpringContext() {
        this.applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(SpringMainConfig.class);
        applicationContext.refresh();
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
